package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *     后端banner 管理的接口
 *
 * </p>
 *
 * @author testjava
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin//为了实现跨域
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    //1.分页查询banner
    @ApiOperation(value = "分页查询")
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit){
        Page<CrmBanner> pageBanner=new Page<>(page,limit);
        crmBannerService.page(pageBanner,null);
        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    //2.添加
    @ApiOperation(value = "添加")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //查询id
    @GetMapping("get/{id}")
    public R get(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("items",crmBanner);
    }

    //3.修改
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public R update(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    //4.删除
    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public R delete(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }
}

